package com.j.lemon.learn.basic.file;


import java.io.*;

public class FileTest {
    public static void main(String[] args) throws IOException {
        File f = new File(File.separator+"Users"+File.separator+"lijunjun"+File.separator+"haha.txt");
        try {
            if(!f.exists()){
                f.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        File [] files = f.listFiles(new FileFilter() {
//            @Override
//            public boolean accept(File pathname) {
//                return pathname.getName().startsWith(".");
//            }
//        });
//        for(File file:files){
//            System.out.println(file);
//        }
        System.out.println(f.exists());
        System.out.println("文件长度为："+f.length());
        RandomAccessFile randomAccessFile = new RandomAccessFile(f,"rw");
        byte [] b = "helloworld".getBytes();
//        randomAccessFile.write(b);
        randomAccessFile.writeBytes("22");
        randomAccessFile.writeBytes("33");
        randomAccessFile.writeBytes("qwe");
//        randomAccessFile.write(b);


        randomAccessFile.seek(randomAccessFile.getFilePointer()-5);
        randomAccessFile.writeBytes("hahaha");
        System.out.println(randomAccessFile.getFilePointer());
//        randomAccessFile.seek(0);
        System.out.println(randomAccessFile.getFilePointer());
//        randomAccessFile.skipBytes(2);
        randomAccessFile.writeBytes("hehe");
//        byte t [] = new byte[8];
//        randomAccessFile.read(t);
//        System.out.println(new String(t));
//        randomAccessFile.seek(0);
//        randomAccessFile.skipBytes(2);
//        System.out.println(randomAccessFile.read());
        byte bb = (byte) randomAccessFile.read();
        String bbb = "";
        System.out.println(bb);
        byte[] qwe = new byte[1024];
        int has = 0;
        while((has=randomAccessFile.read(qwe))>0){
            System.out.println(new String(qwe,0,has));
        }

        randomAccessFile.close();

//        String test = "test";
//        byte tests [] = test.getBytes();
//        for(byte byt:tests){
//            System.out.println(byt);
//        }
//        char cha=test.charAt(0);
//        System.out.println(cha);
//        byte b1 = 104;
//        char asd = (char) b1;
//        char qaz=123;
//        System.out.println(asd);
//        System.out.println(qaz);
        FileOutputStream fos=new FileOutputStream(f,true);
//        fos.write("lijunjun".getBytes());
//        fos.close();
        FileInputStream fis=new FileInputStream(f);
//        byte fiss []= new byte[1024];
//        int l=0;
//        while((l=fis.read(fiss))!=-1){
//            System.out.println(l);
//            System.out.println(new String(fiss,0,l));
//        }
        BufferedOutputStream bof = new BufferedOutputStream(fos);
        bof.write("q".getBytes());
        bof.flush();
        BufferedInputStream bif = new BufferedInputStream(fis);
        byte[] bifs = new byte[1024];
        int bifInt;
        while((bifInt=bif.read(bifs))!=-1){
            System.out.println(new String(bifs,0,bifInt));
        }
        fos.close();
        fis.close();
        bof.close();
        bif.close();

    }

}
